package com.Redis.SpringBoot.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @RequestMapping("/testString")
//    @RequestBody
    public String testString(){
        redisTemplate.opsForValue().set("key","02");
        String key = redisTemplate.opsForValue().get("key");
        System.out.println(key);
        redisTemplate.delete("key");
        return "OK";
    }
    @RequestMapping("/testList")
    public String testList(){
        redisTemplate.opsForList().leftPush("key","02");
        redisTemplate.opsForList().leftPushAll("key","a","b","c");
        List<String> key = redisTemplate.opsForList().range("key", 1, 2);
        System.out.println(key);
        return "ok";
    }
    @RequestMapping("/testHashmap")
    public String testHashmap(){
        redisTemplate.opsForHash().put("key","name","zhangsan");
        String value = (String)redisTemplate.opsForHash().get("key", "name");
        System.out.println(value);
        //这行代码已经把key给删除了
        redisTemplate.opsForHash().delete("key","name");
        return "ok";
    }
    @RequestMapping("/testSet")
    public String testSet(){
        redisTemplate.opsForSet().add("key","aaa","ccc");
        return "ok";
    }
    @RequestMapping("/testZSet")
    public String testZSet(){
        //这里是先value 再分数
        redisTemplate.opsForZSet().add("key","吕布",100);
        redisTemplate.opsForZSet().add("key","赵云",98);
        redisTemplate.opsForZSet().add("key","典韦",96);
        Set<String> key = redisTemplate.opsForZSet().range("key", 0, 2);
        System.out.println(key);
        Long key1 = redisTemplate.opsForZSet().count("key", 96, 100);
        System.out.println(key1);
        return "ok";


    }
    
}
